package br.com.sgdee.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.security.SimplePrincipal;

//import org.jboss.security.SimplePrincipal;

//import org.springframework.security.core.context.SecurityContextHolder;

public class Util {
	
	
	/**
	 * 
	 * @return
	 */
	public static FacesContext getFacesContext() {
		FacesContext fx = FacesContext.getCurrentInstance();
		return fx;
	}
	
	/**
	 * 
	 * @return
	 */
	public static ELContext getElContext() {
		ELContext elContext = getFacesContext().getELContext();
		return elContext;
	}

	/**
	 * 
	 * @return
	 */
	public static ExpressionFactory getExpressionFactory() {
		ExpressionFactory factory = getFacesContext().getApplication().getExpressionFactory();
		return factory;
	}
	
	/**
	 * 
	 * @return
	 */
	public static HttpServletRequest getHttpServletRequest() {
		return (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
	}
	
	/**
	 * 
	 * @return
	 */
	public static HttpServletResponse getHttpServletResponse() {
		return (HttpServletResponse) getFacesContext().getExternalContext().getResponse();
	}
	
	/**
	 * ExternalContext
	 * @return
	 */
	public static ExternalContext getExternalContext() {
		return getFacesContext().getExternalContext();
	}
	
	/**
	 * User name JAAS
	 * @return
	 */
	public static String getUserNameLoginJAAS() {
		Object userPrincipal =  getFacesContext().getExternalContext().getUserPrincipal();

		if(userPrincipal == null) {
			return null;
		}
		return ((SimplePrincipal)userPrincipal).getName();
	}
	
//	public static org.springframework.security.core.userdetails.User getUserSpringSecurity() {
//		org.springframework.security.core.userdetails.User user =
//				(org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().
//				getAuthentication().getPrincipal();
//		return user;
//	}
	
	/**
	 * 
	 * @return
	 */
	public static Object getUserLogado() {
		return null;
	}
	
	public String somenteNumeroCPF( String cpfNumber ) {
		cpfNumber = cpfNumber.replace(".", "");
		cpfNumber = cpfNumber.replace("-", "");
		
		return cpfNumber;
	}
	
	/**
	 * Recuperar Senha criptografada
	 * 
	 * @param senhaNormal
	 * @return
	 */
	public static String recuperarSenhaCriptografada(String senhaNormal) {
		
		return org.jboss.security.auth.spi.Util
				.createPasswordHash("SHA-256", "BASE64", null, null, senhaNormal);
	}
	
	
	/**
	 * Redireciona página outcome via código java
	 * 
	 * @param from
	 * @param outCome
	 */
	public static void redirecionamentoViaNavigation(String from, String outCome) {
		
		getFacesContext().getApplication().getNavigationHandler().handleNavigation(getFacesContext(), from, outCome);
	}
}
